14. Interface and Applications

Reflection

Group Assignment

Attached is the link to group assignment

Individual Assignment

Learning from Serial Communication and UI Design

During this project, I deepened my understanding of user interface development and serial communication. Here are the key concepts and tools I utilized:

Tool/Concept Description
PyQt Designer Used for designing the graphical user interface for my application.
PyQt Serial Port Implemented for handling serial communication between the interface and the Arduino, enabling commands to control the hardware directly from the UI.

Design and Fabrication Process

In this project I did more of the design of the interface where there is only two buttons. One to turn the servo one way and another to move it back. I will be using this to control the motion of my ball release in my final project.

Programming Processes

I utilized PyQt Designer for the user interface layout, I dragged the buttons I needed and changed their size accordingly and saved the UI File.
Vector Tools


Vector Tools

then converted ui file into py file import the Python scripts into Pycharm


Vector Tools


Vector Tools

and then added new codes to manage serial communications with the Arduino when buttons pressed.
Vector Tools

Source Code and Design Files

For Python UI in Pycharm

    # -*- coding: utf-8 -*-
    from PyQt5 import QtCore, QtGui, QtWidgets
    import serial  # Import the serial library
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
    
            self.pushButton = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton.setGeometry(QtCore.QRect(260, 220, 251, 131))
            font = QtGui.QFont()
            font.setPointSize(22)
            self.pushButton.setFont(font)
            self.pushButton.setObjectName("pushButton")
    
            self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton_2.setGeometry(QtCore.QRect(260, 360, 251, 131))
            font = QtGui.QFont()
            font.setPointSize(16)
            self.pushButton_2.setFont(font)
            self.pushButton_2.setObjectName("pushButton_2")
    
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
            # Serial port setup
            self.serial_port = serial.Serial(port='COM7', baudrate=9600, timeout=.1)
            self.pushButton.clicked.connect(self.activate_servo)
            self.pushButton_2.clicked.connect(self.stop_servo)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.pushButton.setText(_translate("MainWindow", "Turn AntiClockwise"))
            self.pushButton_2.setText(_translate("MainWindow", "Turn Back"))
    
        def activate_servo(self):
            self.serial_port.write(b'1')  # Sending '1' to activate the servo
    
        def stop_servo(self):
            self.serial_port.write(b'0')  # Sending '0' to stop the servo
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    
        
For Esp32
    #include 

        Servo myservo;
        int pos = 0; // variable to store the servo position
        
        void setup() {
          myservo.attach(20);  // attach the servo on pin 9 to the servo object
          Serial.begin(9600);  // start serial communication at 9600bps
        }
        
        void loop() {
          if (Serial.available() > 0) {
            int command = Serial.read();  // read the incoming byte
            if (command == '1') {
              // Rotate servo
              for (pos = 0; pos <= 180; pos += 1) { 
                myservo.write(pos);             
                delay(15);                       
              }
            } else if (command == '0') {
              // Stop servo (return to position 0)
              myservo.write(0);
            }
          }
        }
        
        

Troubleshooting and Problem Solving

I encountered several issues during the development, especially with establishing a stable serial connection between my PC and the Arduino. Initially, I faced challenges because the serial port was occupied by the Arduino IDE, which prevented my Python script from accessing it. After closing the Arduino IDE, I was able to establish a connection and successfully control the hardware using my application.

Project Files and Documentation

I have included all coded files in this zip file.

Final Product Showcase

Here is my hero shot. Where I pressed the button on my Pc and it sends a command to ESP32 to activate the servo.



-->